﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skybot.ReadAloud
{
    /// <summary>
    /// 读字符.
    /// </summary>
    /// <remarks>参见:http://www.cnblogs.com/fx_guo/archive/2011/01/14/1935388.html
    /// https://msdn.microsoft.com/zh-cn/library/system.speech.synthesis.speechsynthesizer(v=vs.110).aspx
    /// </remarks>
    public class TTSSpeechSample
    {
        /// <summary>
        /// 开始说
        /// </summary>
        public void StartSpeech()
        {
            //提供对已安装的语音合成引擎的功能的访问
            using (System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer())
            {
                //获取已经安装的语音
                var voices = synth.GetInstalledVoices();
                var curvoic = synth.Voice;

                synth.Volume = 100;

                //设置文件名
                synth.SelectVoice(voices[0].VoiceInfo.Name);


                //输出到声音文件
                synth.SetOutputToWaveFile(AppDomain.CurrentDomain.BaseDirectory + "bb.wav");

                synth.Speak("English");

                synth.Speak("中文,根据语音的排序位置选择具有特定性别和年龄的语音");

                //可以使用它创建语音验证码




            }


        }


        /// <summary>
        /// 在线说话
        /// </summary>
        /// <param name="text"></param>
        public void Speak(string text)
        {

            System.Threading.Tasks.Task.Factory.StartNew(() =>
           {


               //提供对已安装的语音合成引擎的功能的访问
               using (System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer())
               {
                   List<string> strings = new List<string>();
                   if (!text.Contains("\r\n"))
                   {
                       text = text.Replace("\r", string.Empty).Replace("\r", string.Empty);

                       for (int n = 0; n < text.Length; n++)
                       {

                           string str = new string(text.Skip(n * 10).Take(10).ToArray());
                           if (str.Length < 1)
                           {
                               break;
                           }

                           strings.Add(str);

                       }
                   }
                   else
                   {
                       strings = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                   }

                   foreach (var p in strings)
                   {
                       synth.Speak(p);
                   }
               }
           });
        }

        /// <summary>
        /// 语音验证码
        /// </summary>
        public void SpeakCode()
        {

            //提供对已安装的语音合成引擎的功能的访问
            using (System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer())
            {
                byte[] codeBytes = null;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    synth.SetOutputToWaveStream(ms);
                    synth.Speak("验证码为:ABCF");
                    codeBytes = ms.ToArray();
                }


            }
        }
    }
}
